package com.dede.lib;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @author hsh
 * @date 2017/10/9 16:20.
 * @doc
 */
public class Test {

    private String path;

    public Test(String path) {
        this.path = path;
    }

    private int toSize(byte[] bytes) {
        return bytes[0] << 21
                | bytes[1] << 14
                | bytes[2] << 7
                | bytes[3];
    }

    private int toSize2(byte[] data, int offset) {
        return data[offset] << 24
                | data[offset + 1] << 16
                | data[offset + 2] << 8
                | data[offset + 3];

    }

    int getFrameSize(byte[] data) {
        return data[0] << 24
                | data[1] << 16
                | data[2] << 8
                | data[3];
    }

    public void read() {
        File file = new File(path);
        try {
            RandomAccessFile accessFile = new RandomAccessFile(file, "r");

            byte[] bytes = new byte[3];
            int length = accessFile.read(bytes);
            String s = new String(bytes, 0, length);
            System.out.println("head:" + s);
            System.out.println("version:" + accessFile.read());
            System.out.println("reversion:" + accessFile.read());
            System.out.println("flag:" + accessFile.read());
            System.out.println("=============");

            byte[] size = new byte[4];
            accessFile.read(size);
            int l = toSize(size);//标签大小，所有标签帧长度
            System.out.println("frames length:" + l);
            System.out.println("=============");

            while (accessFile.getFilePointer() < l + 10) {
                byte[] frameId = new byte[4];
                accessFile.read(frameId);
                System.out.println("frame id:" + new String(frameId));

                byte[] frameSize = new byte[4];
                accessFile.read(frameSize);
                int frameSize1 = getFrameSize(frameSize);
                if (frameSize1 < 0) {
                    break;
                }
                System.out.println("frame size:" + frameSize1);

                accessFile.skipBytes(2);//跳过标志

                byte[] c = new byte[frameSize1];
                accessFile.read(c);
                System.out.println("frame content:" + new String(c,"gbk"));
                System.out.println("----------------------");
            }

            accessFile.close();
//            byte[] data = new byte[l - 10];
//            accessFile.read(data);
//            int index = 0;
//            while (index < data.length) {
//                System.out.println(new String(data, index, 4));
//                index += 4;
//                int nameSize = toSize2(data, index);
//                index += 4;
//                index += 2;
//                System.out.println(nameSize);
//                System.out.println(new String(data, index, nameSize));
//                index += nameSize;
//                if (index < 0)
//                    break;
//            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
